package edenmedical;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

public class FrmMain {
	
	private static FrmMain istanza;
	private Display display;
	private Shell shell;
	private Label lblStatoBilancia,lblStatoPressione,lblStatoOmron,lblStatoSat300;
	
	public FrmMain (){
		System.out.println("****************************************************");
		System.out.println("Directory corrente:"+System.getProperty("user.dir"));
		System.out.println("****************************************************");
		display = Display.getCurrent();
		shell = new Shell(display);
		
		shell.addListener(SWT.Close,new Listener() {
			public void handleEvent(Event arg0) {
				Shell shell2 = new Shell();
				MessageBox message = new MessageBox(shell2,SWT.ICON_QUESTION |SWT.YES | SWT.NO);
				message.setText("Uscita");
				message.setMessage("Sei sicuro di uscire dall'applicazione?");
				int risp = message.open();
				if (risp==SWT.NO)
				{
					arg0.doit=false;
				}
				else
				{
					shell.dispose();
					istanza=null;
				}
			}
		});
		
		GridLayout gridLayout = new GridLayout();
		shell.setLayout(gridLayout);
		shell.setText("Eden Medical");
		gridLayout.numColumns = 4;
		gridLayout.horizontalSpacing = 100;
		gridLayout.verticalSpacing = 15;
		gridLayout.marginHeight = 20;
		gridLayout.marginWidth = 20;
		
		Button btnSendBilancia = new Button(shell, SWT.NONE);
		btnSendBilancia.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		btnSendBilancia.setText("Test Send");
		btnSendBilancia.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				Channel channel = new Channel();
				ChannelMessage message = new ChannelMessage("3");
				message.addParameter("patient_id","1");
				message.addParameter("selection", "3");
				//ora metto invece i parametri della singola misura
				message.addParameter("measure[weight]","80");
				SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
				message.addParameter("measure[date_measure]", format.format(new Date()));
				message.addParameter("measure[device]", "BILANCIA");
				message.addParameter("measure[bat_change]", "12");
				message.addParameter("commit", "Create Measure");
				channel.send(message);
			}
		});
		
		Button buttonSendPressione = new Button(shell, SWT.NONE);
		buttonSendPressione.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		buttonSendPressione.setText("Test Send");
		buttonSendPressione.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				Channel channel = new Channel();
				ChannelMessage message = new ChannelMessage("3");
				message.addParameter("patient_id","1");
				message.addParameter("selection", "3");
				//ora metto invece i parametri della singola misura
				message.addParameter("measure[massima]","80");
				message.addParameter("measure[minima]","120");
				message.addParameter("measure[meanArterialPressure]","100");
				SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
				message.addParameter("measure[date_measure]", format.format(new Date()));
				message.addParameter("measure[device]", "MIS_PRESSIONE");
				message.addParameter("measure[bat_change]", "12");
				message.addParameter("commit", "Create Measure");
				channel.send(message);
			}
		});
		
		Button buttonSendOmron = new Button(shell, SWT.NONE);
		buttonSendOmron.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		buttonSendOmron.setText("Test Send");
		buttonSendOmron.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				Channel channel = new Channel();
				ChannelMessage message = new ChannelMessage("3");
				message.addParameter("patient_id","1");
				message.addParameter("selection", "3");
				//ora metto invece i parametri della singola misura
				message.addParameter("measure[ossimetria]","80");
				message.addParameter("measure[pulsazioni]","60");
				SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
				message.addParameter("measure[date_measure]", format.format(new Date()));
				message.addParameter("measure[device]", "OMRON");
				message.addParameter("measure[bat_change]", "12");
				message.addParameter("commit", "Create Measure");
				channel.send(message);
			}
		});
		
		Button buttonSendSat300 = new Button(shell, SWT.NONE);
		buttonSendSat300.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		buttonSendSat300.setText("Test Send");
		buttonSendSat300.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				Channel channel = new Channel();
				ChannelMessage message = new ChannelMessage("3");
				message.addParameter("patient_id","1");
				message.addParameter("selection", "3");
				//ora metto invece i parametri della singola misura
				message.addParameter("measure[ossimetria]","90");
				message.addParameter("measure[pulsazioni]","70");
				SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
				message.addParameter("measure[date_measure]", format.format(new Date()));
				message.addParameter("measure[device]", "SAT300");
				message.addParameter("measure[bat_change]", "12");
				message.addParameter("commit", "Create Measure");
				channel.send(message);
			}
		});
		
		Label lblTitolo = new Label(shell,SWT.NONE);
		lblTitolo.setText("EDEN MEDICAL");
		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gd.horizontalSpan=4;
		lblTitolo.setLayoutData(gd);
		
		Label lblBilancia = new Label(shell,SWT.NONE);
		lblBilancia.setText("A&&D\r\nBilancia");
		lblBilancia.setAlignment(SWT.CENTER);
		lblBilancia.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Label lblPressione = new Label(shell,SWT.NONE);
		lblPressione.setText("A&&D\r\nMis.Pressione");
		lblPressione.setAlignment(SWT.CENTER);
		lblPressione.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Label lblOmron = new Label(shell,SWT.NONE);
		lblOmron.setText("Omron\r\nPulsossimetro");
		lblOmron.setAlignment(SWT.CENTER);
		lblOmron.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Label lblSat300 = new Label(shell,SWT.NONE);
		lblSat300.setText("SAT 300\r\nPulsossimetro");
		lblSat300.setAlignment(SWT.CENTER);
		lblSat300.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Image imgBilancia = new Image(display, "img/Bilancia.jpg");
		
		Label lblBilanciaImage = new Label(shell,SWT.NONE);
		lblBilanciaImage.setImage(imgBilancia);
		lblBilanciaImage.setAlignment(SWT.CENTER);
		lblBilanciaImage.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Image imgPressione = new Image(display, "img/Pressione.jpg");
		
		Label lblMisPressioneImage = new Label(shell,SWT.NONE);
		lblMisPressioneImage.setImage(imgPressione);
		lblMisPressioneImage.setAlignment(SWT.CENTER);
		lblMisPressioneImage.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Image imgOmron = new Image(display, "img/Omron.jpg");
		
		Label lblPulsossimetro = new Label(shell,SWT.NONE);
		lblPulsossimetro.setImage(imgOmron);
		lblPulsossimetro.setAlignment(SWT.CENTER);
		lblPulsossimetro.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Image imgSat300 = new Image(display, "img/Sat300.jpg");
		
		Label lblPulsossimetro2 = new Label(shell,SWT.NONE);
		lblPulsossimetro2.setImage(imgSat300);
		lblPulsossimetro2.setAlignment(SWT.CENTER);
		lblPulsossimetro2.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Button btnPinBliancia = new Button(shell,SWT.PUSH);
		btnPinBliancia.setText("Inserisci PIN");
		btnPinBliancia.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		btnPinBliancia.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmPIN frmPIN = new FrmPIN(display,"Bilancia");
					frmPIN.Show();
				}	
		});
		
		Button btnPinPressione = new Button(shell,SWT.PUSH);
		btnPinPressione.setText("Inserisci PIN");
		btnPinPressione.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		btnPinPressione.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmPIN frmPIN = new FrmPIN(display,"Pressione");
					frmPIN.Show();
				}	
		});
		
		Button btnPinOmron = new Button(shell,SWT.PUSH); 
		btnPinOmron.setText("Inserisci PIN");
		btnPinOmron.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		btnPinOmron.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmPIN frmPIN = new FrmPIN(display,"Omron");
					frmPIN.Show();
				}	
		});
		
		Button btnPinSat300 = new Button(shell,SWT.PUSH);
		btnPinSat300.setText("Inserisci PIN");
		btnPinSat300.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		btnPinSat300.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmPIN frmPIN = new FrmPIN(display,"Sat300");
					frmPIN.Show();
				}	
		});
		
		Label lblStato1 = new Label(shell,SWT.NONE);
		lblStato1.setText("Stato");
		lblStato1.setAlignment(SWT.CENTER);
		lblStato1.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Label lblStato2 = new Label(shell,SWT.NONE);
		lblStato2.setText("Stato");
		lblStato2.setAlignment(SWT.CENTER);
		lblStato2.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Label lblStato3 = new Label(shell,SWT.NONE);
		lblStato3.setText("Stato");
		lblStato3.setAlignment(SWT.CENTER);
		lblStato3.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Label lblStato4 = new Label(shell,SWT.NONE);
		lblStato4.setText("Stato");
		lblStato4.setAlignment(SWT.CENTER);
		lblStato4.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
	    lblStatoBilancia = new Label(shell,SWT.NONE);
	    boolean PINTrovato=false;
	    try {
			FileInputStream file = new FileInputStream("data/Bilancia.txt");
			byte[] pin= new byte[8];
			file.read(pin);
			PINTrovato=true;			
    		}
		catch (Exception err) {
    		System.out.println("Errore: " + err);
    		}
		if (PINTrovato)
			lblStatoBilancia.setText("Non Associato");
		else
			lblStatoBilancia.setText("PIN Mancante");
		lblStatoBilancia.setAlignment(SWT.CENTER);
		lblStatoBilancia.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		lblStatoPressione = new Label(shell,SWT.NONE);
		PINTrovato=false;
	    try {
			FileInputStream file = new FileInputStream("data/Pressione.txt");
			byte[] pin= new byte[8];
			file.read(pin);
			PINTrovato=true;			
    		}
		catch (Exception err) {
    		System.out.println("Errore: " + err);
    		}
		if (PINTrovato)
			lblStatoPressione.setText("Non Associato");
		else
			lblStatoPressione.setText("PIN Mancante");
		lblStatoPressione.setAlignment(SWT.CENTER);
		lblStatoPressione.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		lblStatoOmron = new Label(shell,SWT.NONE);
		PINTrovato=false;
	    try {
			FileInputStream file = new FileInputStream("data/Omron.txt");
			byte[] pin= new byte[8];
			file.read(pin);
			PINTrovato=true;			
    		}
		catch (Exception err) {
    		System.out.println("Errore: " + err);
    		}
		if (PINTrovato)
			lblStatoOmron.setText("Non Associato");
		else
			lblStatoOmron.setText("PIN Mancante");
		lblStatoOmron.setAlignment(SWT.CENTER);
		lblStatoOmron.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		lblStatoSat300 = new Label(shell,SWT.NONE);
		PINTrovato=false;
	    try {
			FileInputStream file = new FileInputStream("data/Sat300.txt");
			byte[] pin= new byte[8];
			file.read(pin);
			PINTrovato=true;			
    		}
		catch (Exception err) {
    		System.out.println("Errore: " + err);
    		}
		if (PINTrovato)
			lblStatoSat300.setText("Non Associato");
		else
			lblStatoSat300.setText("PIN Mancante");
		lblStatoSat300.setAlignment(SWT.CENTER);
		lblStatoSat300.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		Button btnStoricoBilancia = new Button(shell,SWT.PUSH);
		btnStoricoBilancia.setText("Mostra Storico");
		btnStoricoBilancia.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		btnStoricoBilancia.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmStoricoPeso frmStorico = new FrmStoricoPeso("Bilancia");
					frmStorico.Show();
				}	
		});
		
		Button btnStoricoPressione = new Button(shell,SWT.PUSH); 
		btnStoricoPressione.setText("Mostra Storico");
		btnStoricoPressione.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		btnStoricoPressione.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmStoricoPressione frmStorico = new FrmStoricoPressione("Pressione");
					frmStorico.Show();
				}	
		});
		
		Button btnStoricoOmron = new Button(shell,SWT.PUSH); 
		btnStoricoOmron.setText("Mostra Storico");
		btnStoricoOmron.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		btnStoricoOmron.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmStoricoPulsossimetro frmStorico = new FrmStoricoPulsossimetro("Omron",DeviceType.OMRON);
					frmStorico.Show();
				}	
		});
		
		Button btnStoricoSat300 = new Button(shell,SWT.PUSH); 
		btnStoricoSat300.setText("Mostra Storico");
		btnStoricoSat300.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		btnStoricoSat300.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmStoricoPulsossimetro frmStorico = new FrmStoricoPulsossimetro("Sat300",DeviceType.SAT300);
					frmStorico.Show();
				}	
		});
		
		Button btnUltimoDatoBilancia = new Button(shell,SWT.PUSH); 
		btnUltimoDatoBilancia.setText("Visualizza Ultimo Dato");
		btnUltimoDatoBilancia.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		btnUltimoDatoBilancia.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmVisDatiPeso frmVisDati = new FrmVisDatiPeso("Bilancia",getUltimaMisura(DeviceType.BILANCIA));
					frmVisDati.Show();
				}	
		});
		
		Button btnUltimoDatoPressione = new Button(shell, SWT.NONE);
		btnUltimoDatoPressione.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		btnUltimoDatoPressione.setText("Visualizza Ultimo Dato");
		btnUltimoDatoPressione.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmVisDatiPressione frmVisDati = new FrmVisDatiPressione("Pressione",getUltimaMisura(DeviceType.MIS_PRESSIONE));
					frmVisDati.Show();
				}	
		});
		
		Button btnUltimoDatoOmron = new Button(shell, SWT.NONE);
		btnUltimoDatoOmron.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		btnUltimoDatoOmron.setText("Visualizza Ultimo Dato");
		btnUltimoDatoOmron.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmVisDatiPulsossimetro frmVisDati = new FrmVisDatiPulsossimetro("Omron",getUltimaMisura(DeviceType.OMRON),DeviceType.OMRON);
					frmVisDati.Show();
				}	
		});
		
		Button btnUltimoDatoSat300 = new Button(shell, SWT.NONE);
		btnUltimoDatoSat300.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		btnUltimoDatoSat300.setText("Visualizza Ultimo Dato");
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		
		MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
		mntmFile.setText("File");
		
		Menu menu_1 = new Menu(mntmFile);
		mntmFile.setMenu(menu_1);
		
		MenuItem mntmEsci = new MenuItem(menu_1, SWT.NONE);
		mntmEsci.addListener(SWT.Selection,new Listener() {
	    	public void handleEvent(Event e) {
	    		Shell shell2 = new Shell();
				MessageBox message = new MessageBox(shell2,SWT.ICON_QUESTION |SWT.YES | SWT.NO);
				message.setText("Uscita");
				message.setMessage("Sei sicuro di uscire dall'applicazione?");
				int risp = message.open();
				if (risp==SWT.YES)
				{
					shell.dispose();
					istanza=null;
				}
	    	}
	    });
		
		mntmEsci.setText("Esci");
		btnUltimoDatoSat300.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event e)
				{
					FrmVisDatiPulsossimetro frmVisDati = new FrmVisDatiPulsossimetro("Sat300",getUltimaMisura(DeviceType.SAT300),DeviceType.SAT300);
					frmVisDati.Show();
				}	
		});
	}
	
	public void Show(){
		shell.open();
		shell.pack();
		Display display = Display.getCurrent();
		Monitor primary = display.getPrimaryMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = shell.getBounds();
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    shell.setLocation(x, y);
		while( !shell.isDisposed())
		{
			if(!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
	
	public String[] getUltimaMisura(DeviceType deviceType)
	{
		String[] message=null;
		try{
		FileReader os = null;
		switch (deviceType)
		{
		case BILANCIA:
			os = new FileReader("data/dbBilancia.txt");
			break;
		case MIS_PRESSIONE:
			os = new FileReader("data/dbMisPressione.txt");
			break;
		case OMRON:
			os = new FileReader("data/dbOmron.txt");
			break;
		case SAT300:
			os = new FileReader("data/dbSat300.txt");
			break;
		}
		
		BufferedReader in = new BufferedReader(os);
		String line=null;
		while ((line = in.readLine())!=null)
		{
			message = line.split(";");
		}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return message;
	}
	
	public static FrmMain getInstance()
	{
		if (istanza == null)
			istanza = new FrmMain();
		return istanza;
	}
	
	public void setLblStatoBilancia(String stato)
	{
		lblStatoBilancia.setText(stato);
	}
	
	public void setLblStatoPressione(String stato)
	{
		lblStatoPressione.setText(stato);
	}
	
	public void setLblStatoOmron(String stato)
	{
		lblStatoOmron.setText(stato);
	}
	
	public void setLblStatoSat300(String stato)
	{
		lblStatoSat300.setText(stato);
	}
}


